package utils

import (
	cRand "crypto/rand"
	"math/big"
	"math/rand"
	"time"
)

type CodeTypeEnum int

const (
	CodeTypeNum   CodeTypeEnum = 0 // 纯数字
	CodeTypeLOWER CodeTypeEnum = 1 // 小写字母
	CodeTypeUPPER CodeTypeEnum = 2 // 大写字母
	CodeTypeALL   CodeTypeEnum = 3 // 数字、大小写字母
)

func Rand(size int, codeType CodeTypeEnum, isCrypt bool) string {
	// 数字，小写字母，大写字母范围
	kinds, result := [][]int{{48, 10}, {97, 26}, {65, 26}}, make([]byte, size)
	var randType bool
	if codeType > 2 || codeType < 0 {
		randType = true

	}
	rand.Seed(time.Now().UnixNano())
	for i := 0; i < size; i++ {
		if randType {
			codeType = CodeTypeEnum(rand.Intn(int(CodeTypeALL)))
		}
		base, max := kinds[codeType][0], kinds[codeType][1]
		var randomNum int
		if isCrypt {
			bigIntValue, _ := cRand.Int(cRand.Reader, big.NewInt(int64(max)))
			randomNum = int(bigIntValue.Int64())
		} else {
			randomNum = rand.Intn(max)
		}

		result[i] = uint8(base + randomNum)
	}
	return string(result)
}
